//
//  HTGoodsTypeView.m
//  TruckUser
//
//  Created by Ting Han on 2017/11/1.
//  Copyright © 2017年 Ting Han. All rights reserved.
//

#import "HTGoodsTypeView.h"
#import "HTGoodsTypeBoardView.h"

#define boardHeight (kWidth(450)+IPHONE_SAFEBOTTOMAREA_HEIGHT)

@interface HTGoodsTypeView ()<HTGoodsTypeBoardViewDelegate>

@property (nonatomic,strong) HTGoodsTypeBoardView *goodsTypeBoard;
@property (nonatomic,strong) UIView *backView;

@end

@implementation HTGoodsTypeView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.frame = [UIScreen mainScreen].bounds;
        [self setupView];
    }
    return self;
}
- (void)setupView{
    self.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.5];
    self.backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kHTScreenWidth, (kHTScreenHeight - boardHeight))];
    self.backView.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.1];
    [self addSubview:self.backView];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismissView)];
    [self.backView addGestureRecognizer:tap];
    [self addSubview:self.goodsTypeBoard];
    [self.goodsTypeBoard.closeButton addTarget:self action:@selector(dismissView) forControlEvents:UIControlEventTouchUpInside];
}

- (HTGoodsTypeBoardView *)goodsTypeBoard{
    if (!_goodsTypeBoard) {
        _goodsTypeBoard = [[NSBundle mainBundle]loadNibNamed:@"HTGoodsTypeBoardView" owner:self options:nil].firstObject;
        _goodsTypeBoard.frame = CGRectMake(0, kHTScreenHeight, kHTScreenWidth, boardHeight);
        _goodsTypeBoard.delegate = self;
    }
    return _goodsTypeBoard;
}
- (void)didChoseItem:(NSString *)item{
    if (self.delegate && [self.delegate respondsToSelector:@selector(didChoseTypeItem:)]) {
        [self.delegate didChoseTypeItem:item];
    }
    [self dismissView];
}
-(void)showView{
    
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [UIView animateWithDuration:.3 animations:^{
        self.goodsTypeBoard.frame =  CGRectMake(0, kHTScreenHeight - boardHeight, kHTScreenWidth, boardHeight);
        [self layoutIfNeeded];
    }];
}
-(void)dismissView {
    self.backView.alpha = 0;
    [UIView animateWithDuration:.3 animations:^{
        self.goodsTypeBoard.frame =  CGRectMake(0, kHTScreenHeight, kHTScreenWidth, boardHeight);
        self.alpha = 0;
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        [self removeFromSuperview];
    }];
}

@end
